/**********************************************************************
 * LeechCraft - modular cross-platform feature rich internet client.
 * Copyright (C) 2006-2014  Georg Rudoy
 *
 * Distributed under the Boost Software License, Version 1.0.
 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
 **********************************************************************/

#pragma once

#include "findnotificationwe.h"
#include <QtWebEngineCore/QWebEnginePage>
#include <QtWebEngineWidgets/QWebEngineView>
#include <QtWebEngineCore/QWebEngineFindTextResult>

namespace SailmiPIM::Util
{
/** @brief Constructs the find notification using the given
         * proxy and near widget.
         *
         * @param[in] proxy The core proxy to be used by this find
         * notification.
         * @param[in] near The web view near which to embed.
         *
         * @sa FindNotification
         */
FindNotificationWE::FindNotificationWE (const ICoreProxy_ptr& proxy, QWebEngineView *near)
    : FindNotification { proxy, near }
    , WebView_ { near }
{
}


FindNotificationWE::~FindNotificationWE ()
{

}

void FindNotificationWE::HandleNext (const QString& text, FindFlags findFlags)
{
    if (PreviousFindText_ != text)
    {
        ClearFindResults ();
        PreviousFindText_ = text;
    }

    WebView_->page ()->findText (text, ToPageFlags (findFlags),
                               [this] (const QWebEngineFindTextResult& found) { SetSuccessful (found.numberOfMatches ()); });
}

void FindNotificationWE::Reject ()
{
    FindNotification::Reject ();
    ClearFindResults ();
}

void FindNotificationWE::ClearFindResults ()
{
    PreviousFindText_.clear ();
    WebView_->page ()->findText ({});
}

}
